/*
 * NamedColorRangeHSV.cpp
 *
 *  Created on: Jan 2, 2010
 *      Author: vuk
 */

#include "NamedColorRangeHSV.h"

NamedColorRangeHSV::NamedColorRangeHSV(const char *file)
:name(file)
{
//	class BadFile{};
	ifstream f(file);
	if(!f)
	{
		low.set();
		hi.set();
		represent.set();
		f.close();
		ofstream of(file);
		save();
//		for(int i=0; i<9; i++)
//			of<<"0"<<endl;
		of.close();
		f.open(file);
	}
//	name=*file;
	else
	{
		int data[9];

		for(int i=0; i<9; i++)
			f>>data[i];
		low.set(data[0], data[1], data[2]);
		hi.set(data[3], data[4], data[5]);
		represent.set(data[6], data[7], data[8]);
	}
	cout<<"Boja uzeta iz fajla:"<<endl<<*this;
//	string str;
//
//	f>>str;
//	if (str!="Crvena_donja:")	throw BadFile();
//	f>>h>>s>>v;
//	rl.set(h, s, v);
	f.close();
}

void NamedColorRangeHSV::save()
{
	ofstream of(name.c_str());
		of << endl << low << hi << represent;
	of.close();
}
void NamedColorRangeHSV::config(const char &taster)
{
//	namedWindow("Stelovanje boje: ", CV_WINDOW_AUTOSIZE);
	//	Mat colorframe(frame.size(), CV_8UC3);
//	Mat colorframe;
//	resize(frame, colorframe, Size(160,120), 2, 2, 0);
//	cvtColor(colorframe, colorframe, CV_BGR2HSV);


	switch(taster)
	{
	//Taster za snimanje trenutnih podesavanja boje
		case('+'):save();break;

	//Tasteri za stelovanje za po jedan, ili sa SHIFT za po 10


		//Tasteri za stelovanje donje granice
		case('A'):low.addH(10); cout<<*this; break;
		case('a'):low.addH(1); cout<<*this; break;
		case('z'):low.subH(1); cout<<*this; break;
		case('Z'):low.subH(10); cout<<*this; break;

		case('S'):low.addS(10); cout<<*this; break;
		case('s'):low.addS(1); cout<<*this; break;
		case('x'):low.subS(1); cout<<*this; break;
		case('X'):low.subS(10); cout<<*this; break;

		case('D'):low.addV(10); cout<<*this; break;
		case('d'):low.addV(1); cout<<*this; break;
		case('c'):low.subV(1); cout<<*this; break;
		case('C'):low.subV(10); cout<<*this; break;


		//Tasteri za stelovanje gornje granice
		case('F'):hi.addH(10); cout<<*this; break;
		case('f'):hi.addH(1); cout<<*this; break;
		case('v'):hi.subH(1); cout<<*this; break;
		case('V'):hi.subH(10); cout<<*this; break;

		case('G'):hi.addS(10); cout<<*this; break;
		case('g'):hi.addS(1); cout<<*this; break;
		case('b'):hi.subS(1); cout<<*this; break;
		case('B'):hi.subS(10); cout<<*this; break;

		case('H'):hi.addV(10); cout<<*this; break;
		case('h'):hi.addV(1); cout<<*this; break;
		case('n'):hi.subV(1); cout<<*this; break;
		case('N'):hi.subV(10); cout<<*this; break;


		//Tasteri za stelovanje boje koja ce reprezentovati objekat
		case('J'):represent.addH(10); cout<<*this; break;
		case('j'):represent.addH(1); cout<<*this; break;
		case('m'):represent.subH(1); cout<<*this; break;
		case('M'):represent.subH(10); cout<<*this; break;

		case('K'):represent.addS(10); cout<<*this; break;
		case('k'):represent.addS(1); cout<<*this; break;
		case(','):represent.subS(1); cout<<*this; break;
		case('<'):represent.subS(10); cout<<*this; break;

		case('L'):represent.addV(10); cout<<*this; break;
		case('l'):represent.addV(1); cout<<*this; break;
		case('.'):represent.subV(1); cout<<*this; break;
		case('>'):represent.subV(10); cout<<*this; break;
	}

//	for(int i=0; i<colorframe.rows; i++)
//		for(unsigned int j=0; j<colorframe.step ; j+=colorframe.channels())
//		{
//			if(*this==colorframe.data+(i*colorframe.step)+j)
//				fillwidthrepresent(colorframe.data+(i*colorframe.step)+j);
//			else
//				*(colorframe.data+(i*colorframe.step)+j+2)=0;
//		}
//	cvtColor(colorframe, colorframe, CV_HSV2BGR);
//	imshow("Stelovanje boje: ",colorframe);
//	cvDestroyWindow("Stelovanje boje: ");
}

void NamedColorRangeHSV::findobject(int &X, int &Y, int fspace, int erspace, const Mat &frame)
{
	Mat search;
	resize(frame, search, Size(160,120), 2, 2, 0);
	cvtColor(search, search, CV_BGR2HSV);
	uchar *tr=NULL;
	uchar *tl=NULL;
	int AX, AY;
	AX=AY=0;
	int errorcount;
	for(int i=search.rows; i>fspace; i--)
	{
		for(int j=0; j < search.cols/2-fspace/2; j++)
		{
			errorcount=0;
			tr= search.data+( i*search.step + (search.cols/2+j)*search.channels() );
			if(*this==tr)
			{
				for(int up=0; up<fspace; up++)
				{
					for(int s=0; s<fspace/2; s++)
					{

						tr= search.data+( (i-up)*search.step + (search.cols/2+j+s) * search.channels() );
						//if(c.r(tr) || c.g(tr) || c.b(tr))
						if(*this!=tr)
							errorcount++;

						tr= search.data+( (i-up)*search.step + (search.cols/2+j-s) * search.channels());
						//if(c.r(tr) || c.g(tr) || c.b(tr))
						if(*this!=tr)
							errorcount++;

						if(errorcount>=erspace)
							break;
					}
					if(errorcount>=erspace)
						break;

				}
				if(errorcount<erspace)
				{
					AX=j;
					AY=search.rows-i;
					break;
				}
			}

			if(AY)
				break;

			tl= search.data+(i*search.step - (search.cols/2+j)*search.channels() );

//			//if(!c.r(tl) && !c.g(tl) && !c.b(tl))
			if(*this==tl)
			{

				for(int up=0; up<fspace; up++)
				{
					for(int s=0; s<fspace/2; s++)
					{

						tl= search.data+( (i-up)*search.step - (search.cols/2+j+s) * search.channels() );

						//if(c.r(tl) || c.g(tl) || c.b(tl))
						if(*this!=tl)
							errorcount++;

						tl= search.data+((i-up)*search.step - (search.cols/2+j-s) * search.channels());

						//if(c.r(tl) || c.g(tl) || c.b(tl))
						if(*this!=tl)
							errorcount++;

						if(errorcount>=erspace)
							break;
					}
					if(errorcount>=erspace)
						break;
				}

				if(errorcount<erspace)
				{
					AX=-j;
					AY=search.rows-i;
					break;
				}
			}

		}
		if(AY)
			break;

	}
	X=AX;
	Y=AY;
}
void NamedColorRangeHSV::findobstacle(int &X, int &Y, int fspace, int erspace, const Mat &frame)
{
	Mat search;
	resize(frame, search, Size(160,120), 2, 2, 0);
	cvtColor(search, search, CV_BGR2HSV);
	uchar *tr=NULL;
	uchar *tl=NULL;
	int AX, AY;
	AX=AY=0;
	int errorcount;
	for(int i=search.rows; i>fspace; i--)
	{
		for(int j=0; j < search.cols/2-fspace/2; j++)
		{
			errorcount=0;
			tr= search.data+( i*search.step + (search.cols/2+j)*search.channels() );
			if(*this!=tr)
			{
				for(int up=0; up<fspace; up++)
				{
					for(int s=0; s<fspace/2; s++)
					{

						tr= search.data+( (i-up)*search.step + (search.cols/2+j+s) * search.channels() );
						//if(c.r(tr) || c.g(tr) || c.b(tr))
						if(*this==tr)
							errorcount++;

						tr= search.data+( (i-up)*search.step + (search.cols/2+j-s) * search.channels());
						//if(c.r(tr) || c.g(tr) || c.b(tr))
						if(*this==tr)
							errorcount++;

						if(errorcount>=erspace)
							break;
					}
					if(errorcount>=erspace)
						break;

				}
				if(errorcount<erspace)
				{
					AX=j;
					AY=search.rows-i;
					break;
				}
			}

			if(AY)
				break;

			tl= search.data+(i*search.step - (search.cols/2+j)*search.channels() );

//			//if(!c.r(tl) && !c.g(tl) && !c.b(tl))
			if(*this!=tl)
			{

				for(int up=0; up<fspace; up++)
				{
					for(int s=0; s<fspace/2; s++)
					{

						tl= search.data+( (i-up)*search.step - (search.cols/2+j+s) * search.channels() );

						//if(c.r(tl) || c.g(tl) || c.b(tl))
						if(*this==tl)
							errorcount++;

						tl= search.data+((i-up)*search.step - (search.cols/2+j-s) * search.channels());

						//if(c.r(tl) || c.g(tl) || c.b(tl))
						if(*this==tl)
							errorcount++;

						if(errorcount>=erspace)
							break;
					}
					if(errorcount>=erspace)
						break;
				}

				if(errorcount<erspace)
				{
					AX=-j;
					AY=search.rows-i;
					break;
				}
			}

		}
		if(AY)
			break;

	}
	X=AX;
	Y=AY;
}


//NamedColorRangeHSV::NamedColorRangeHSV()
//{
//	// TODO Auto-generated constructor stub
//
//}
//
//NamedColorRangeHSV::~NamedColorRangeHSV()
//{
//	// TODO Auto-generated destructor stub
//}
